﻿<cfsilent>
	<cfscript>
		
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");

		chooseProp = getProperty("serviceFactory").getBean("choosePropertyAdvice");
		timeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");
		
		sequenceProgramTask = getProperty("serviceFactory").getBean("sequenceProgramTask");
		sequenceStudentCourse = getProperty("serviceFactory").getBean("sequenceStudentCourse");
		
		response = structNew();
		structInsert(response, "success", false, true);
		
		termId = event.getArg("TermID", event.getArg("TID"));
		
		/* 学生相关信息 */
		sql = "	SELECT 
					a.stu_id,
					a.cls_id,
					a.sbj_direction,
					b.student_prop 
				FROM t_student a 
					INNER JOIN t_student_status b ON b.stu_id = a.stu_id 
				WHERE 
					a.stu_id = :studentId ";

		queryObj = new Query( datasource=application.dnsSlave ) ;
		queryObj.addParam( name="studentId", value=event.getArg("STU"), cfsqltype="cf_sql_varchar" );
		rs_student= queryObj.execute( sql=sql ).getResult();
		
		/* 学籍相关信息 */
		studentId = rs_student.stu_id;
		classId = rs_student.cls_id;
		studentDir = rs_student.sbj_direction;
		
		if ( mid( rs_student.student_prop, 1, 1) eq "1" ) {
		
			transaction {
			
				timeStamp = timeAdvice.getCurrentDatetimeString();
			
				/* 载入班级必修课课程任务 */
				sql = "SELECT
							a.tsk_id, a.cid, a.cidx, a.task_classes 
						FROM
							t_task a 
							INNER JOIN t_course b ON b.cid = a.cid 
						WHERE
							a.term_id = :termId 
							AND
							b.prop_id = '1'";
				
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
				
				
				rs_task = queryObj.execute( sql=sql ).getResult();
				
				for( a = 1; a le rs_task.recordCount; a++ ){
					
					courseId = rs_task["cid"][a];
					courseIdx = rs_task["cidx"][a];
					taskId = rs_task["tsk_id"][a];
					
					for( className in listToArray(rs_task["task_classes"][a], ",")) {
				
						
						sql = "SELECT a.cls_id FROM t_class a 
							  		INNER JOIN t_term b on b.year_term = a.grade 
							  WHERE a.cls_name = :className 
							  AND 
							  b.term_id = :termId";
							  
						queryObj = new Query( datasource=application.dnsMaster );
						queryObj.addParam( name="className", value=className, cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
						
						rs_class = queryObj.execute( sql=sql ).getResult(); 
						
						
						sql = "	SELECT
									stu.stu_id,stu.sbj_direction
								FROM
									t_student stu INNER JOIN t_student_status ss ON ss.stu_id = stu.stu_id
								WHERE
									EXISTS(
										SELECT 1 FROM i_stu_class_info b WHERE b.cls_id = :classId AND b.stu_id = stu.stu_id 
									)
									AND
									SUBSTR(ss.student_prop,1,1) = '1' 
									AND stu.stu_id = :studentId ";
						
						queryObj = new Query( datasource=application.dnsMaster );
						
						queryObj.addParam( name="classId", value=rs_class.cls_id, cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="studentId", value=studentId, cfsqltype="cf_sql_varchar" );
						
						rs_student = queryObj.execute( sql=sql ).getResult();
						
						for( b = 1; b le rs_student.recordCount; b++ ){
							//writeLog( text=rs_student["stu_id"][b]);
							//writeLog( text=courseId);
							
							sequenceStudentCourse.setStudentID(rs_student["stu_id"][b]);
							sequenceStudentCourse.setCourseID(courseId);
							sequenceStudentCourse.setCourseIdx(courseIdx);
							sequenceStudentCourse.setTermID(termId);
							sequenceStudentCourse.setTaskType("P");

							chooserId = sequenceStudentCourse.getID();

							sql = "SELECT
										sc.scc_id,sc.choose_prop
									FROM
										t_student_course sc
									WHERE
										sc.scc_id = :chooserId ";
							
							queryObj = new Query( datasource=application.dnsMaster );
							
							queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
							
							rs_studentCourse = queryObj.execute( sql=sql ).getResult();
							
							if( rs_studentCourse.recordCount eq 0 ){
							
								prop = chooseProp.init();
							
								/* 预置课表是否锁定 */
								//if ( yesNoFormat(event.getArg('Locked')) ) {
									prop.setLocked(true);
								//} else {
								//	prop.setLocked(false);
								//}
								/* 缴费状态设置 */
								//if ( yesNoFormat(event.getArg('PrePaid')) ) {
									prop.setPrePaid( true ); /* 是否从预交学费扣除 */
									prop.setPaid( true ); /* 是否已收费 */
								//} else {
								//	prop.setPrePaid( false );
								//	prop.setPaid( false );
								//}

								/* 没有选课记录 */
								

								sql = "INSERT INTO 
										t_student_course
										  (scc_id,
										   stu_id,
										   cid,
										   term_id,
										   prop_id,
										   task_type,
										   cidx,
										   task_id,
										   st,
										   retaking,
										   ct,
										   choose_time,
										   choose_prop,
										   charge_time)
										VALUES
										  (
										   :chooserId,
										   :studentId,
										   :courseId,
										   :termId,
										   :propId,
										   :taskType,
										   :courseIndex,
										   :taskId,
										   :studyType,
										   :retaking,
										   :chooseTurn,
										   :chooseTime,
										   :chooseProp,
										   :chargeTime 
										  )";
								
								queryObj = new Query( datasource=application.dnsMaster );
								
								queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
								queryObj.addParam( name="studentId", value=rs_student["stu_id"][b], cfsqltype="cf_sql_varchar" );
								queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
								queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
								queryObj.addParam( name="propId", value="1", cfsqltype="cf_sql_char" );
								queryObj.addParam( name="taskType", value="P", cfsqltype="cf_sql_char" );
								queryObj.addParam( name="courseIndex", value=courseIdx, cfsqltype="cf_sql_char" );
								queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
								queryObj.addParam( name="studyType", value="N", cfsqltype="cf_sql_char" );
								queryObj.addParam( name="retaking", value="0", cfsqltype="cf_sql_char" );
								queryObj.addParam( name="chooseTurn", value="9", cfsqltype="cf_sql_char" );
								queryObj.addParam( name="chooseTime", value=timeStamp, cfsqltype="cf_sql_char" );
								queryObj.addParam( name="chooseProp", value=prop.stringProperty(), cfsqltype="cf_sql_varchar" );
								queryObj.addParam( name="chargeTime", value=timeStamp, cfsqltype="cf_sql_char" );
								
								queryObj.execute( sql=sql );
								
							} else {
							
								/* 更新选课记录 */
								prop = chooseProp.init();
								prop.parseProperty( rs_studentCourse.choose_prop );
								/* 预置课表是否锁定 */
								if ( yesNoFormat(event.getArg('Locked')) ) {
									prop.setLocked(true);
								} else {
									prop.setLocked(false);
								}
								/* 缴费状态设置 */
								if ( yesNoFormat(event.getArg('PrePaid')) ) {
									prop.setPrePaid( true ); /* 是否从预交学费扣除 */
									prop.setPaid( true ); /* 是否已收费 */
								} else {
									prop.setPrePaid( false );
									prop.setPaid( false );
								}
							
								/*Unique: STU_ID, CID, TERM_ID, TASK_TYPE*/
								
								sql = "	UPDATE
											t_student_course sc
										SET
											sc.choose_prop = :chooseProp ,
											sc.cidx = :courseIndex ,
											sc.task_id = :taskId ,
											sc.st = :studyType ,
											sc.retaking = :retaking ,
											sc.choose_time = :chooseTime ,
											sc.charge_time = :chargeTime ,
											sc.ct = :chooseTurn ,
											sc.prop_id = :propId 
										WHERE
											sc.scc_id = :chooserId ";
								
								queryObj = new Query( datasource=application.dnsMaster );
								
								queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
								queryObj.addParam( name="chooseProp", value=prop.stringProperty(), cfsqltype="cf_sql_varchar" );
								queryObj.addParam( name="courseIndex", value=courseIdx, cfsqltype="cf_sql_char" );
								queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
								queryObj.addParam( name="studyType", value="N", cfsqltype="cf_sql_char" );
								queryObj.addParam( name="retaking", value="0", cfsqltype="cf_sql_char" );
								queryObj.addParam( name="chooseTime", value=timeStamp, cfsqltype="cf_sql_char" );
								queryObj.addParam( name="chargeTime", value=timeStamp, cfsqltype="cf_sql_char" );
								queryObj.addParam( name="chooseTurn", value="9", cfsqltype="cf_sql_char" );
								queryObj.addParam( name="propId", value="1", cfsqltype="cf_sql_char" );
								
								queryObj.execute( sql=sql );
								
							}
						}
						
						
						/* 完成一个班级后 刷新数据记录 */
					
						sql = "	SELECT
									t.tsk_id
								FROM
									t_task t
								WHERE
									t.tsk_id = :taskId ";
						
						queryObj = new Query( datasource=application.dnsMaster );
						
						queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
						
						rs_taskEntity = queryObj.execute( sql = sql ).getResult();
						
						if (rs_taskEntity.recordCount) {
							
							/* 每完成一批班级学生添加 刷新一次教学班已选人数 */
							sql = "	SELECT
										COUNT(*) r_count
									FROM
										t_student_course sc
									WHERE
										sc.task_id = :taskId ";
							
							queryObj = new Query( datasource=application.dnsMaster );
							
							queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
							
							rs_taskCountEntity = queryObj.execute( sql = sql ).getResult();
	
							if( rs_taskCountEntity.recordCount ){
								sql = "	UPDATE
											t_task t
										SET 
											t.task_chooesd = :realSize 
										WHERE
											t.tsk_id = :taskId ";
											
								queryObj = new Query( datasource=application.dnsMaster );
								
								queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
								queryObj.addParam( name="realSize", value=rs_taskCountEntity.r_count, cfsqltype="cf_sql_integer" );
								
								queryObj.execute( sql = sql );
							}
						}
							
					}
				}
			
			}
			structInsert(response, "success", true, true);
		}
		else {
			structInsert(response, "reason", "学生学籍标注为不在校, 不可待选必修课", true);
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>